package codingforgreat.lchot150;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//不难 没看
public class Lc015 {

       public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        int sum = 0;
        for(int i = 0;i < nums.length;i++){
            if(nums[i] > 0) return result;
            if(i > 0 && nums[i] == nums[i - 1]) continue;
            int l = i + 1;
            int r = nums.length - 1;
            while(l<r){
                sum = nums[i] + nums[l] + nums[r];
                if(sum == 0){
                    List list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[l]);
                    list.add(nums[r]);
                    result.add(list);
                    while(l<r && nums[l+1] == nums[l]) l++;
                    while (l<r && nums[r-1] == nums[r]) r--;
                    ++l;
                    --r;
                }else if(sum < 0) {
                    l++;
                } else {
                    r--;
                }
            }
        }return result;
    }
}
